2132. Point on a line

 

Determine whether a point belongs to the line given by equation Ax + By + C = 0.

 

Input. Five integers – the coordinates of the point x, y and the coefficients A, B, C of the line equation (it is guaranteed that A and B are not simultaneously 0).

 

Output. Print “YES”, if the point belongs to the line and “NO” otherwise.

 

Sample input

Sample output

3 7 -2 1 -1

YES

 

 

SOLUTION

geometry

 

Algorithm analysis

Point (x0, y0) belongs to the line Ax + By + C = 0 if and onlt if

Ax0 + By0 + C = 0

 

Algorithm realization

Lets check whether a point belongs to a line by substituting its coordinates into the line equation.

 

scanf("%d %d %d %d %d",&x,&y,&a,&b,&c);

if (a*x + b*y + c) printf("NO\n"); else printf("YES\n");

 

Algorithm realization – structure, Ñ

 

#include <stdio.h>

 

struct Point

{

  int x, y;

};

 

struct Line

{

  int a, b, c;

};

 

int OnLine(struct Point p, struct Line l)

{

  return (l.a * p.x + l.b * p.y + l.c == 0);

}

 

int main(void)

{

  struct Point p;

  scanf("%d %d", &p.x, &p.y);

 

  struct Line l;

  scanf("%d %d %d", &l.a, &l.b, &l.c);

  if (OnLine(p,l)) printf("YES\n"); else printf("NO\n");

 

  return 0;

}

 

Algorithm realization – classes

 

#include <stdio.h>

 

class Point

{

public:

  int x, y;

  Point(int x = 0, int y = 0) : x(x), y(y) {};

};

 

class Line

{

public:

  int a, b, c;

  Line(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c) {};

  int OnLine(Point &p)

  {

    return (a*p.x + b * p.y + c == 0);

  }

};

 

int main(void)

{

  int x, y;

  scanf("%d %d", &x, &y);

  Point p(x, y);

 

  int a, b, c;

  scanf("%d %d %d", &a, &b, &c);

  Line l(a, b, c);

  if (l.OnLine(p)) printf("YES\n"); else printf("NO\n");

 

  return 0;

}

 

Java realization – classes

 

import java.util.*;

 

class Point

{

  int x, y;

  Point(int x, int y)

  {

    this.x = x;

    this.y = y;

  }

}

 

class Line

{

  int a, b, c;

  Line(int a, int b, int c)

  {

    this.a = a;

    this.b = b;

    this.c = c;

  }

  

  boolean OnLine(Point p)

  {

    return a*p.x + b*p.y + c == 0;

  }

}

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int x = con.nextInt(), y = con.nextInt();

    Point p = new Point(x,y);

   

    int a = con.nextInt(), b = con.nextInt(), c = con.nextInt();

    Line l = new Line(a,b,c);

 

    if (l.OnLine(p))

      System.out.printf("YES");

    else

      System.out.printf("NO");

    con.close();

  }

}